package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.ReportMapper;
import com.sky.mapper.WorkspaceMapper;
import com.sky.result.Result;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private WorkspaceMapper workspaceMapper;
    @Autowired
    private ReportMapper reportMapper;
    /**
     * 今日运营数据
     * @return
     */
    @Override
    public BusinessDataVO businessData(LocalDateTime min,LocalDateTime max) {

//        private Double turnover;//营业额
        Double turnover = workspaceMapper.getTurnover(min,max);
        if(turnover == null){
            turnover = 0.0;
        }
//        private Integer validOrderCount;//有效订单数
        Integer validOrderCount = workspaceMapper.getValidOrderCount(min,max);
//        private Double orderCompletionRate;//订单完成率
        Integer number = workspaceMapper.getAllOrders(min,max);
        Double orderCompletionRate = 0.0;
        if(validOrderCount != 0){
            orderCompletionRate = (validOrderCount + 0.0) / number;
        }

//        private Double unitPrice;//平均客单价
        Double unitPrice = 0.0;
        if(validOrderCount != 0){
            unitPrice = turnover / validOrderCount;
        }

//        private Integer newUsers;//新增用户数
        Integer newUsers = workspaceMapper.newUsers(min,max);
        return BusinessDataVO.builder()
                .newUsers(newUsers)
                .unitPrice(unitPrice)
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
    }

    /**
     * 套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO overviewSetmeals() {
//        // 已启售数量
//        private Integer sold;
        Integer sold = workspaceMapper.sold();
//        // 已停售数量
//        private Integer discontinued;
        Integer discontinued = workspaceMapper.discontinued();
        return SetmealOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 菜品总览
     * @return
     */
    @Override
    public DishOverViewVO overviewDishes() {
        // 已启售数量
//        private Integer sold;
        Integer sold = workspaceMapper.soldDish();
        // 已停售数量
//        private Integer discontinued;
        Integer discontinued = workspaceMapper.discontinuedDish();
        return DishOverViewVO.builder()
                .discontinued(discontinued)
                .sold(sold)
                .build();
    }

    /**
     * 查询订单
     * @return
     */
    @Override
    public OrderOverViewVO orderOverViewVO() {
        //待接单数量
//        private Integer waitingOrders;
        Integer waitingOrders = workspaceMapper.getOrderByStatus(Orders.REFUND);
        //待派送数量
//        private Integer deliveredOrders;
        Integer deliveredOrders = workspaceMapper.getOrderByStatus(Orders.DELIVERY_IN_PROGRESS);
        //已完成数量
//        private Integer completedOrders;
        Integer completedOrders = workspaceMapper.getOrderByStatus(Orders.COMPLETED);
        //已取消数量
//        private Integer cancelledOrders;
        Integer cancelledOrders = workspaceMapper.getOrderByStatus(Orders.CANCELLED);
        //全部订单
//        private Integer allOrders;
        Integer allOrders = workspaceMapper.getOrders();
        return OrderOverViewVO.builder()
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .deliveredOrders(deliveredOrders)
                .allOrders(allOrders)
                .waitingOrders(waitingOrders)
                .build();
    }
}
